/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UI;

import Utility.EmptyNullCheckUtiliy;
import controller.EmployeeController;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import logging.CommonsLogging;
import org.apache.log4j.Logger;
import vo.rb_ot_str;

/**
 *
 * @author Shuhail
 */
public class AddOvertimeHours extends javax.swing.JFrame {

    /**
     * Creates new form AddOvertimeHours
     */
    public AddOvertimeHours() {
        initComponents();
        populateCombo();
        this.populateOtherData(this.empTypeCombo.getSelectedItem().toString());
    }
    
    public void populateOtherData(String s) {
        boolean isNew = true;
        
         if(isNew) {
            //check if its already saved but not persisted
            if(otList.size()>0) {
                Iterator it1 = otList.iterator();
                while(it1.hasNext()) {
                    
                    rb_ot_str otstr = (rb_ot_str)it1.next();
                    if(otstr.getEmployee_type().equals(s)){
                        isNew=false;
                        this.otText.setText(Double.toString(otstr.getOt_amt_per_hr()));
                    }
                }
            }    
                        }
         if(isNew){
        Iterator it = list.iterator();
        while(it.hasNext()) {
            rb_ot_str ot = (rb_ot_str)it.next();
            if(ot.getEmployee_type().equals(s)) {
                this.otText.setText(Double.toString(ot.getOt_amt_per_hr()));
                isNew=false;
                this.messageLabel.setText("");
            }
        }
         }
            if(isNew){            
            this.messageLabel.setText("New Employee Type. Enter the OT rate");
            this.otText.setText("");
            }
        }
    
    
    public boolean saveAllData() {
        if(logger.isDebugEnabled()) {
            logger.debug("***Starting fetching data for overtime");
        }
        double ot_rate = -1;
        if(!EmptyNullCheckUtiliy.isEmptyOrNull(this.otText.getText()))
        ot_rate = Double.parseDouble(this.otText.getText());
        
         if(logger.isDebugEnabled()) {
            logger.debug("Rate:::"+ot_rate);
        }
         if(EmptyNullCheckUtiliy.isEmptyOrNull(ot_rate) || ot_rate==-1) {
             
             this.messageLabel.setText("Please Enter The Rate");
             return false;
         }
         rb_ot_str ot = new rb_ot_str();
         ot.setEmployee_type(this.empTypeCombo.getSelectedItem().toString());
         ot.setOt_amt_per_hr(ot_rate);
         otList.add(ot);
          if(logger.isDebugEnabled()) {
            logger.debug("**"+ot.getEmployee_type()+"**"+ot.getOt_amt_per_hr());
            logger.debug("**Total "+otList.size()+" records");
        }
       
         return true;
    }
    public void populateCombo() {
        
        List<String> list = EmployeeController.getInstance().getEmployeeType();
        Iterator it = list.iterator();
        while(it.hasNext()) {
            
            String empType = (String)it.next();
            empTypeCombo.addItem(empType);
    }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        empTypeCombo = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        otText = new javax.swing.JTextField();
        messageLabel = new javax.swing.JLabel();
        saveButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        okButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Add New Overtime Hour(s)");
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("Employee Type");

        empTypeCombo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                empTypeComboItemStateChanged(evt);
            }
        });

        jLabel2.setText("Amount per Hour");

        otText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                otTextActionPerformed(evt);
            }
        });

        saveButton.setText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        okButton.setText("Cancel");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        okButton1.setText("OK");
        okButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(40, 40, 40)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(empTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(otText, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(messageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(saveButton)
                        .addGap(28, 28, 28)
                        .addComponent(okButton1)
                        .addGap(29, 29, 29)
                        .addComponent(okButton)))
                .addContainerGap(100, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(empTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(otText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addComponent(messageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(okButton)
                    .addComponent(okButton1))
                .addContainerGap(40, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void otTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_otTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_otTextActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        // TODO add your handling code here:
        dispose();
        
    }//GEN-LAST:event_okButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        // TODO add your handling code here:
        if(saveAllData())
            this.messageLabel.setText("Succesfully Added");
        
    }//GEN-LAST:event_saveButtonActionPerformed

    private void empTypeComboItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_empTypeComboItemStateChanged
        // TODO add your handling code here:
        populateOtherData(this.empTypeCombo.getSelectedItem().toString());
    }//GEN-LAST:event_empTypeComboItemStateChanged

    private void okButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButton1ActionPerformed
        // TODO add your handling code here:
                saveAllData();
        if(this.otList.size()>0) {
            if(logger.isDebugEnabled()) {
                logger.debug("Have "+otList.size()+" revords to indert");
                logger.debug("Persisiting");
            }
            EmployeeController.getInstance().insertMultipleOTS(otList);
        }
        dispose();

    }//GEN-LAST:event_okButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AddOvertimeHours.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AddOvertimeHours.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AddOvertimeHours.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AddOvertimeHours.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AddOvertimeHours().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox empTypeCombo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel messageLabel;
    private javax.swing.JButton okButton;
    private javax.swing.JButton okButton1;
    private javax.swing.JTextField otText;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables
    
    private List<rb_ot_str> otList = new ArrayList<rb_ot_str>();
    Logger logger = CommonsLogging.getInstance().getLogger();
    List<rb_ot_str> list = EmployeeController.getInstance().getAllEmployeeTypeAndOT();
}
